const baseData = {
  start_time: '',
  end_time: '',
  title: '',
  cateid: '',
  page: '',
  limit: 10,
};
// /ydd_article/list
const layer = layui.layer
async function render() {
  const { data: { data: res } } = await axios.get('/ydd_article/list', {
    params: baseData
  })
  // console.log(res.data);
  // console.log(res)
  const htmlStr = res.data.map((ele) => {
    let { cateid, title, ishot, istop, pic, create_date, catename, id, default_data } = ele
    let time = new Date(create_date)
    let text
    catename = catename || ''
    if (default_data === '0') {
      text = '待审核'
    } else {
      text = '已发布'
    }
    time = time.toLocaleString()
    return `<tr>
                <td>${id}</td>
                <td>${title}</td>
                <td>${catename}</td>
                <td><img src="http://124.223.14.236:8060/${pic}" alt="" style="width: 120px;"></td>
                <td>${time}</td>
                <td><input type="checkbox" name="ishot" lay-skin="switch"  ${ishot === 0 ? '' : 'checked'} disabled></td>
                <td><input type="checkbox" name="istop" lay-skin="switch"  ${istop === 0 ? '' : 'checked'} disabled></td>
                <td><span>${text}</span></td>
                <td style="width: 100px;">
                  <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" data-id="${id}"><i
                      class="layui-icon">&#xe642;</i></button>
                  <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger delBtn" data-id="${id}"><i
                      class="layui-icon">&#xe640;</i></button>
                </td>
              </tr>
            `
  }).join('')
  // console.log(htmlStr)
  $('.tbody').html(htmlStr)
  renderPage(res.count)
  render1()
}
render();
// ========删除功能
$('.tbody').on('click', '.delBtn', function () {
  // alert(1)
  const id = $(this).attr('data-id')
  layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, async function (index) {
    //do something
    await axios.get('/ydd_article/del', { params: { id } })
    layer.msg('删除成功')
    render()
    layer.close(index);
  });
})
// ============分页
let laypage = layui.laypage;
function renderPage(count) {
  //执行一个laypage实例
  laypage.render({
    elem: 'page-box', //注意，这里的 test1 是 ID，不用加 # 号
    count,//数据总数，从服务端得到
    limit: baseData.limit,
    limits: [10, 20, 50, 80, 100],
    curr: baseData.page,
    layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
    jump: function (obj, first) {
      //obj包含了当前分页的所有参数，比如：
      // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
      // console.log(obj.limit); //得到每页显示的条数
      // console.log(first)
      baseData.page = obj.curr
      baseData.limit = obj.limit
      //首次不执行
      if (!first) {
        //do something
        // console.log('a');
        render()
      }
    }
  });
}

let flag = true
$('#btn1').click(function () {
  let r = 0
  if (flag) {
    $('#header').hide()
    flag = false
    r += 180;
    $('#btn1').css('transform', `rotate(${r}deg)`);
  } else {
    $('#header').show()
    flag = true
    $('#btn1').css('transform', `rotate(0deg)`);
  }
});
// ======================表头复制
async function render1() {
  const form = layui.form
  const { data: { data } } = await axios.get('/ydd_cate/all')
  console.log(data);
  const newArr = data.map(ele => ele.catename)
  console.log(newArr)
  const htmlStr = newArr.map((ele, index) => ` <option value="${index}">${ele}</option>`).join('')
  $('[name=city]').html(htmlStr)
  form.render('select')
}
// ==============导出
$('.outBtn').click(function () {

  var table = document.getElementById("tabInfo")
  var data = []
  for (var i = 0, row; row = table.rows[i]; i++) {
    var rowData = []
    for (var j = 0, col; col = row.cells[j]; j++) {
      rowData.push(col.innerText)
    }
    data.push(rowData)
  }
  // 创建工作簿
  var workbook = XLSX.utils.book_new()

  // 创建工作表
  var worksheet = XLSX.utils.aoa_to_sheet(data)

  // 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1")

  // 导出工作簿为 Excel 文件
  XLSX.writeFile(workbook, "数据.xlsx")
})
// ===============搜索
$('.searchBtn').on('click',function(){
  const textName = $('[name=textName]').val()
  baseData.title = textName

  render()
  // baseData.title = ''
 })
$('.serchForm').on('submit',function(e){
  e.preventDefault()
})
$('.reset').click(function(){
   baseData.title = ''
   baseData.page = 1
  render()
})
